package com.sttg.csp.common.service;

import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sttg.csp.common.dao.LoginDAO;
import com.sttg.csp.common.exception.BizException;

/**
 * 
 * This class is responsible for dealing with login service.
 */
public class LoginService {

	/**
	 * Logger for this class
	 */
	private final Log logger = LogFactory.getLog(LoginService.class);

	private static final LoginService instance = new LoginService();    

	private LoginDAO loginDAO = LoginDAO.getInstance();

	   
	/** 
	 * Private the default constructor.
	 */   
	private LoginService() {}    
   
	/** 
	 * * Static factory methods.
	 */   
	public static LoginService getInstance() {    
		return instance;    
	}    

	/**
	 * deal with user login
	 * @param user
	 * @return current user info
	 * @throws BizException
	 */
	public Map doUserLogin(Map param) throws BizException {
		String uid = (String) param.get("BD_UID");
		String userType = (String) param.get("USER_TYPE");
		
		logger.debug("UID of current user is: " + uid);
		logger.debug("User type of current user is: " + userType);
		
		Map user = null;
		if ("1".equals(userType)) {
			//null
		} else if ("2".equals(userType)) {
			// current user is B/D admin
			user = loginDAO.queryBDAdminByUserID(uid);
			if (user.isEmpty()) {
				throw new BizException("BD administrator not exists!");
			}
		} else if ("3".equals(userType)) {
			//current user is B/D user
			user = loginDAO.queryBDUserByUserID(uid);
			if (user.isEmpty()) {
				throw new BizException("BD user not exists!");
			}
		} else {
			throw new BizException("The user type is not exists.");
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("User has successfully loged in." + param);
		}
		
		return user;
	}
	
}